/* eslint-disable indent */
import dayjs from 'dayjs'
import config from '../config'
import { robotChat } from '../http/home'
import { updateSessionId } from '@/store/modules/chat'
export const currentTime = (() => {
	return dayjs().format('HH:mm')
})()

// 对话容器发送消息
export const sendChatuiMessage = async (message, chatValue) => {
	const params = {
		Utterance: message.content.text, // 机器⼈访问者的输入
		SessionId: chatValue.sessionId || '',
		...config.chatPrivateParams
	}
	const url = handleQueryStringParams(config.requestBaseUrl, params)
	return robotChat(url, { data: params })
}

// 对话容器接收机器人数据处理
export const handleChatuiMessage = (originMessage, requestType, dispatchCallback) => {
	console.log('message', originMessage)
	const sessionId = originMessage.SessionId
	// 设置sessionId下次对话传参携带此数据
	dispatchCallback(updateSessionId, sessionId)
	const { Messages } = originMessage
	return handleRenderMessage(Messages, originMessage)
}
// 处理问答结果所要展示对应卡片
export function handleRenderMessage(messageList, originMessage) {
	let renderMessage = null
	messageList.forEach((item) => {
		const { AnswerType, Text, AnswerSource } = item
		switch (AnswerType) {
			case 'Text':
				// 文本
				renderMessage = handleTextMessage(Text, originMessage)
				break
			case 'Recommend':
				renderMessage = handleRecommendMessage(AnswerSource)
				// 澄清或者反问
				break
			case 'Knowledge':
				//知识库知识直出
				break
			case 'cardAnswer':
				// 卡⽚⼯坊的卡⽚答案
				break
		}
	})
	return renderMessage
}
// 处理返回对话文本
export function handleTextMessage(Text, originMessage) {
	const { AnswerSource, Content, ContentType } = Text
	let textMessage
	switch (AnswerSource) {
		case 'NO_ANSWER':
			// ⽆答案
			textMessage = {
				type: 'text',
				content: {
					text: Content
				}
			}
			break
		case 'ChitChat':
			// 闲聊
			break
		case 'BotFramework':
			// 对话工厂
			break
		case 'MACHINE_READ':
			// 机器阅读
			break
		case 'CardAnswer':
			// 卡片答案
			break
		case 'SENSITIVE_WORD':
			// 全局敏感词
			break
		case 'COMMANDS':
			// 系统指令
			break
		case 'KBQA':
			// 图谱问答
			break
		case 'TABLEQA':
			// 表格问答
			break
		case 'DATA_ANALYSIS':
			// 数据问答 [⾮标，依赖开启数据问答能⼒]
			textMessage = handleTableMessage(ContentType, Content, originMessage)
			break
	}
	return textMessage
}

// 处理Recommend对话数据
export function handleRecommendMessage(AnswerSource) {
	let recommendMessage
	switch (AnswerSource) {
		case 'BotFramework':
			// 对话⼯⼚澄清
			break
		case 'INTENT_FAQ_CLARIFY':
			// FAQ和对话⼯⼚混合澄清
			break
		case 'KNOWLEDGE':
			// 知识问答
			break
		case 'KBQA':
			// 图谱问答
			break
		case 'TABLEQA':
			// 表格问答
			break
	}
	return recommendMessage
}

// 处理表格数据
export function handleTableMessage(ContentType, Content, originMessage) {
	console.log('Content', JSON.parse(Content))
	const isvMessage = window.isvParser({ data: originMessage })
	console.log('isvMessage', isvMessage)
	let tableMessage
	switch (ContentType) {
		case 'CARD_TEMPLATE':
			tableMessage = isvMessage
			break
	}
	return tableMessage
}

// 处理参数
export const handleQueryStringParams = (baseUrl, params) => {
	const keys = Object.keys(params)
	const query = new URLSearchParams()
	keys.forEach((item) => {
		query.append(item, params[item])
	})
	return `${baseUrl}?${query}`
}
